﻿using CPPEI.ETL.DataServiceInterface;
using CPPEI.WebApi;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using ENT = CPPEI.ETL.Domain.Entities;
using MOD = CPPEI.ETL.Domain.Models;
using SER = CPPEI.ETL.Domain.Services;

namespace CPPEI.ETL.WebApi
{
    [CPPEI.WebApi.AuthPostAction("ETL", "CreateDataTable", "1.0")]
    public class CreateDataTableAction : BaseAction<CreateDataTableParams, CreateDataTableResult>
    {
        protected override ActionResult<CreateDataTableResult> OnExecute(ActionContext context, 
            SysParameters sysParameters,
            CreateDataTableParams opParams)
        {
            SER.DtTableService service = new SER.DtTableService();
            ServiceResult<ENT.EDtTable> serResult = service.Create(context.UserId, opParams.TableName, opParams.ColumnSettings);

            if(serResult.IsSuccess)
            {
                CreateDataTableResult result = new CreateDataTableResult();
                result.TableId = serResult.Data.Id;

                return new ActionResult<CreateDataTableResult>(result);
            }
            else
            {
                return new ActionResult<CreateDataTableResult>(serResult.IsSuccess, serResult.ErrorCode, serResult.Message, null);
            }
        }
    }

    public class CreateDataTableParams
    {
        [Parameter]
        public string TableName { get; set; }

        [Parameter]
        public string ColumnSettings { get; set; }
    }

    public class CreateDataTableResult
    {
        public Guid TableId { get; set; }
    }
}
